/*
 Copyright (c) 2014 by Contributors

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package com.whirl.ai_ml_example;

import com.whirl.ai_ml_example.util.CustomEval;
import ml.dmlc.xgboost4j.java.Booster;
import ml.dmlc.xgboost4j.java.DMatrix;
import ml.dmlc.xgboost4j.java.XGBoost;
import ml.dmlc.xgboost4j.java.XGBoostError;
//import ml.dmlc.xgboost4j.java.example.util.CustomEval;

import java.util.HashMap;

import static com.whirl.util.Constants.XGBoost_PATH;

/**
 * predict first ntree
 *
 * @author hzx
 */
public class PredictFirstNtree {
  public static void main(String[] args) throws XGBoostError {
    // load file from text file, also binary buffer generated by xgboost4j
    DMatrix trainMat = new DMatrix(XGBoost_PATH+"demo/data/agaricus.txt.train");
    DMatrix testMat = new DMatrix(XGBoost_PATH+"demo/data/agaricus.txt.test");

    //specify parameters
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("eta", 1.0);
    params.put("max_depth", 2);
    params.put("silent", 1);
    params.put("objective", "binary:logistic");


    //specify watchList
    HashMap<String, DMatrix> watches = new HashMap<String, DMatrix>();
    watches.put("train", trainMat);
    watches.put("test", testMat);


    //train a booster
    int round = 3;
    Booster booster = XGBoost.train(trainMat, params, round, watches, null, null);

    //predict use 1 tree
    float[][] predicts1 = booster.predict(testMat, false, 1);
    //by default all trees are used to do predict
    float[][] predicts2 = booster.predict(testMat);

    //use a simple evaluation class to check error result
    CustomEval eval = new CustomEval();
    System.out.println("error of predicts1: " + eval.eval(predicts1, testMat));
    System.out.println("error of predicts2: " + eval.eval(predicts2, testMat));
  }
}
